package com.kotlin.base.common

import android.annotation.SuppressLint
import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import java.util.*

/**
 * @author Mr.Wang
 * @version v0.3.6
 * @time 2018/3/12
 * @describe
 */
class AppManager private constructor(){
    private val activityStack:Stack<Activity> = Stack()

    companion object {
        val instance:AppManager by lazy { AppManager() }
    }

    fun addActivity(activity:Activity){
        activityStack.add(activity)
    }
    fun finishActivity(activity:Activity){
        activity.finish()
        activityStack.remove(activity)
    }
    fun currentActivity():Activity{
        return activityStack.lastElement()
    }

    //清理栈
    fun finishAllActivity(){
        for (acitivity in activityStack){
            acitivity.finish()
        }
        activityStack.clear()
    }
@SuppressLint("MissingPermission")
//退出APP
    fun exitApp(context: Context){
        finishAllActivity()
        val activityManager =   context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        activityManager.killBackgroundProcesses(context.packageName)
        System.exit(0)
    }
}